package com.swxy.club.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 易
 */

public class FeignRequestInterceptor  implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        // 确保每个 Feign 请求都带上正确的 token
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            String token = request.getHeader("token");
            if (token != null && !token.isEmpty()) {
                template.header("token", token);
            }
        }
    }
}